package com.matchmealpha;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ShareSettingActivity extends Activity implements
		OnCheckedChangeListener, OnClickListener {

	public static final String MATCHME_LOCATION = "MATCHME_LOCATION";
	public static final String MATCHME_DESCRIPTION = "MATCHME_DESCRIPTION";
	public static final String MATCHME_REQUESTEE = "MATCHME_REQUESTEE ";

	protected static String REQUEST_CODE = "REQUEST_CODE";
	protected static final int GET_FRIEND_REQUEST_CODE = 1;

	private Button leftButton;
	private Button rightButton;
	private TextView errorMsg;
	protected EditText whatText;
	protected EditText whereText;
	protected EditText whoText;
	protected EditText matchmeText;

	private String requester;

	private CheckBox facebook;
	private CheckBox twitter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bund = this.getIntent().getExtras();
		if (bund != null) {
			requester = bund.getString("requester");
		} else {
			requester = "";
		}
		initLayout();
		initListener();
	
	}

	private void initLayout() {
		setContentView(R.layout.sharesetting_view);
		leftButton = (Button) this.findViewById(R.id.SharesettingLeftButton);
		rightButton = (Button) this.findViewById(R.id.SharesettingRightButton);
		errorMsg = (TextView) this.findViewById(R.id.SharesettingError);
		whatText = (EditText) this.findViewById(R.id.SharesettingWhatText);
		whereText = (EditText) this.findViewById(R.id.SharesettingWhereText);
		whoText = (EditText) this.findViewById(R.id.SharesettingWhoText);
		matchmeText = (EditText) this
				.findViewById(R.id.SharesettingMatchMeText);
		if ( requester != null && !requester.equals("") ){
			matchmeText.setText(this.requester);
			matchmeText.setEnabled(false);
		}
		whoText.setEnabled(false);
		errorMsg.setVisibility(View.GONE);
		// matchmeText.setEnabled(false);
	}

	private void initListener() {
		leftButton.setOnClickListener(this);
		rightButton.setOnClickListener(this);
		matchmeText.setOnClickListener(this);

		// facebook.setOnCheckedChangeListener(this);
		// twitter.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		Intent in;
		switch (v.getId()) {
		case (R.id.SharesettingLeftButton):
			in = new Intent();
			this.setResult(RESULT_CANCELED, in);
			this.finish();
			break;
		case (R.id.SharesettingRightButton):
			// ShareGroup.shareGroup.back();
			in = new Intent();
			in.putExtra(ShareSettingActivity.MATCHME_DESCRIPTION, whatText
					.getText().toString());
			in.putExtra(ShareSettingActivity.MATCHME_REQUESTEE, matchmeText
					.getText().toString());
			in.putExtra(ShareSettingActivity.MATCHME_LOCATION, whereText
					.getText().toString());
			this.setResult(RESULT_OK, in);
			this.finish();
			break;
		case (R.id.SharesettingMatchMeText):
			in = new Intent(this, FriendsListActivity.class);
			in.putExtra("back", true);
			in.putExtra("callerID", MatchMeTabWidget.SHARE);
			in.putExtra(REQUEST_CODE, GET_FRIEND_REQUEST_CODE);
			startActivityForResult(in, GET_FRIEND_REQUEST_CODE);
			break;
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == GET_FRIEND_REQUEST_CODE) {
			super.onActivityResult(requestCode, resultCode, data);
			if (resultCode == RESULT_OK) {
				// update the matchme editext field
				Bundle bund = data.getExtras();
				if (bund != null) {
					matchmeText.setText(bund
							.getString(FriendsListAdapter.FRIEND_USERNAME));
//					matchmeText.postInvalidate();
				} else
					matchmeText.setText("MatchMe?");
			}
		}
	}

}
